package com.example.sens.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.example.sens.entity.Performance;
import com.example.sens.entity.User;
import com.example.sens.service.MessageService;
import com.example.sens.service.UserService;
import com.example.sens.utils.Response;
import com.example.sens.vo.PerformanceVo;
import com.example.sens.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 *
 * @date 2020-04-23 13:10
 */

@RestController
@RequestMapping("/api/user")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;

    @PostMapping("/login")
    public Response login(@RequestParam("userName") String userName,
                          @RequestParam("userPass") String userPass,
                          HttpSession session) {
        User user = userService.findByUserName(userName);
        if (user == null) {
            return Response.no("用户名不存在！");
        }
        if (!Objects.equals(user.getUserPass(), userPass)) {
            return Response.no("密码不正确！");
        }
        session.setAttribute("user", user);
        return Response.yes(user);
    }


    @GetMapping("/logout")
    public Response logout(HttpSession session) {
        session.removeAttribute("user");
        return Response.yes();
    }

    @GetMapping("/current-user")
    public Response login(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Response.no("用户未登录");
        }
        Integer unReadSize = messageService.getUnReadSize(user.getUserId());
        user.setUnReadSize(unReadSize);
        return Response.yes(user);
    }

    @PostMapping("/save")
    public Response addUser(@RequestBody User user,HttpSession session) {
        try {
            userService.saveByUser(user,session);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.no(e.getMessage());
        }
        return Response.yes();
    }

    /**
     * 获取分页数据
     * @param userVo
     * @return
     */
    @PostMapping("/getByPage")
    public Response<Page<User>> getByPage(@RequestBody UserVo userVo) {
        Page<User> quizPage = null;
        try {
            quizPage = userService.findAll(new Page(userVo.getPageNo(), userVo.getPageSize()));
        } catch (Exception e) {
            e.printStackTrace();
            return Response.no();
        }
        return Response.yes(quizPage);
    }

    @GetMapping("getById")
    public Response<User> getById(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Response<User> response = new Response<>();
        response.setData(user);
        response.setSuccess(true);
        return response;
    }

    /**
     * 导出Excel
     * @param param
     * @return
     */
    @RequestMapping(value = "exportExcel", method = RequestMethod.POST)
    @ResponseBody
    public Response<String> exportExcel(@RequestBody UserVo param) {
        return userService.exportExcel(param);
    }

}
